package utgin

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
)

type Gin struct {
	Ctx *gin.Context
}

// BaseRsp
// @Description: 通用返回 json数据 {"code": code, "data": data, "msg": msg}
type BaseRsp struct {
	Code int         `json:"code" doc:"错误码"`
	Msg  string      `json:"msg" doc:"错误信息"`
	Data interface{} `json:"data" doc:"数据包"`
}

func (ug *Gin) GetReq(obj interface{}) bool {
	if err := ug.Ctx.ShouldBindBodyWith(&obj, binding.JSON); err != nil {
		ug.UnprocessableEntity(EcBody, "请求参数解析错误!")
		return false
	}
	return true
}

func (ug *Gin) Response(httpStatus int, code int, data interface{}, msg string) {
	ug.Ctx.JSON(httpStatus, gin.H{"code": code, "data": data, "msg": msg})
}

func (ug *Gin) Ok(code int, data interface{}, msg string) {
	ug.Response(http.StatusOK, code, data, msg)
}

func (ug *Gin) OkSuccess(data interface{}, msg string) {
	ug.Ok(0, data, msg)
}

func (ug *Gin) BadRequest(code int, msg string) {
	ug.Response(http.StatusBadRequest, code, nil, msg)
}

func (ug *Gin) InternalServerError(code int, msg string) {
	ug.Response(http.StatusInternalServerError, code, nil, msg)
}

func (ug *Gin) UnprocessableEntity(code int, msg string) {
	ug.Response(http.StatusUnprocessableEntity, code, nil, msg)
}

func (ug *Gin) InternalServerErrorDbMgo() {
	ug.InternalServerError(EcDbMgo, "更新数据失败!")
}

func (ug *Gin) InternalServerErrorGameServer() {
	ug.InternalServerError(EcGameServer, "游戏服务器操作失败!")
}
